package com.ruoyi.canteen.mapper;

import java.util.List;
import com.ruoyi.canteen.domain.CanteenCpOrder;
import com.ruoyi.canteen.vo.OrderShowVo;
import org.apache.ibatis.annotations.*;

/**
 * 主订单Mapper接口
 * 
 * @author zhangw
 * @date 2021-09-19
 */
public interface CanteenCpOrderMapper 
{
    /**
     * 查询主订单
     * 
     * @param id 主订单主键
     * @return 主订单
     */
    public CanteenCpOrder selectCanteenCpOrderById(Long id);

    /**
     * 查询主订单列表
     * 
     * @param canteenCpOrder 主订单
     * @return 主订单集合
     */
    public List<CanteenCpOrder> selectCanteenCpOrderList(CanteenCpOrder canteenCpOrder);

    public List<CanteenCpOrder> selectCanteenCpOrderList1to1(CanteenCpOrder canteenCpOrder);

    /**
     * 新增主订单
     * 
     * @param canteenCpOrder 主订单
     * @return 结果
     */
    public int insertCanteenCpOrder(CanteenCpOrder canteenCpOrder);

    /**
     * 修改主订单
     * 
     * @param canteenCpOrder 主订单
     * @return 结果
     */
    public int updateCanteenCpOrder(CanteenCpOrder canteenCpOrder);

    /**
     * 删除主订单
     * 
     * @param id 主订单主键
     * @return 结果
     */
    public int deleteCanteenCpOrderById(Long id);

    /**
     * 批量删除主订单
     * 
     * @param ids 需要删除的数据主键集合
     * @return 结果
     */
    public int deleteCanteenCpOrderByIds(Long[] ids);

    /**
     * 查询当前用户的订单列表(order_type = 1 有效订单)
     * @param userId 当前用户
     * @return
     */
    @Select("SELECT o.id,o.order_no,order_type,o.md_id,total_price,order_status,o.create_time," +
            "m.md_name,m.md_pic,m.phone from canteen_cp_order as o " +
            "left join canteen_md as m on o.md_id = m.md_id " +
            "where o.pay_flag = 1 and user_id=#{userId} " +
//            "and o.pay_flag=#{payFlag} " +
            "order by create_time desc")
    @Results({
            @Result(property = "orderId", column = "id"),
            @Result(property = "orderNo", column = "order_no"),
            @Result(property = "mdId", column = "md_id"),
            @Result(property = "totalPrice", column = "total_price"),
            @Result(property = "orderStatus", column = "order_status"),
            @Result(property = "dateAdd", column = "create_time"),
            @Result(property = "mdName", column = "md_name"),
            @Result(property = "mdPic", column = "md_pic"),
            @Result(property = "phone", column = "phone")
    })
    public List<OrderShowVo> selectOrderShowVoList(@Param("userId") Long userId,@Param("payFlag")  String payFlag);

    /**
     * 查询当前用户的订单列表(order_type = 1 外卖订单)
     * @param userId 当前用户
     * @param payFlag 付款状态
     * @param day 最近几天
     * @return
     */
    @Select("SELECT o.id,o.order_no,order_type,o.md_id,total_price,order_status,o.create_time,o.user_code," +
            "m.md_name,m.md_pic,m.phone from canteen_cp_order as o " +
            "left join canteen_md as m on o.md_id = m.md_id " +
            "where user_id=#{userId} " +
            "and o.pay_flag=#{payFlag} and DATE_SUB(CURDATE(), INTERVAL #{day} DAY) <= date(create_time)" +
            "order by create_time desc")
    @Results({
            @Result(property = "orderId", column = "id"),
            @Result(property = "orderNo", column = "order_no"),
            @Result(property = "mdId", column = "md_id"),
            @Result(property = "totalPrice", column = "total_price"),
            @Result(property = "orderStatus", column = "order_status"),
            @Result(property = "dateAdd", column = "create_time"),
            @Result(property = "mdName", column = "md_name"),
            @Result(property = "mdPic", column = "md_pic"),
            @Result(property = "phone", column = "phone"),
            @Result(property = "userCode", column = "user_code")
    })
    public List<OrderShowVo> selectDefaultOrderShowVoList(@Param("userId") Long userId,
                                                          @Param("payFlag")  String payFlag, @Param("day") Integer day);

    /**
     * 查询当前用户的主订单
     * @param userId
     * @param orderId
     * @return
     */
    @Select("SELECT o.id,o.order_no,order_type,o.md_id,total_price,order_status,o.create_time," +
            "m.md_name,m.md_pic,m.phone from canteen_cp_order as o " +
            "left join canteen_md as m on o.md_id = m.md_id " +
            "where  user_id=#{userId} and o.id=#{orderId}")
    @Results({
            @Result(property = "orderId", column = "id"),
            @Result(property = "orderNo", column = "order_no"),
            @Result(property = "mdId", column = "md_id"),
            @Result(property = "totalPrice", column = "total_price"),
            @Result(property = "orderStatus", column = "order_status"),
            @Result(property = "dateAdd", column = "create_time"),
            @Result(property = "mdName", column = "md_name"),
            @Result(property = "mdPic", column = "md_pic"),
            @Result(property = "phone", column = "phone")
    })
    public OrderShowVo selectOrderShowVo(@Param("userId") Long userId,@Param("orderId")  Long orderId);

    /**
     * 根据ID查询当前主订单(包含商户号)
     * @param orderId
     * @return
     */
    @Select("SELECT o.id,o.order_no,order_type,o.md_id,total_price,order_status,o.create_time,o.user_code," +
            "c.pos_no as subMchId from canteen_cp_order as o " +
            "left join canteen_jiju_config as c on o.md_id = c.canteen_id " +
            "where o.id=#{orderId}")
    @Results({
            @Result(property = "orderId", column = "id"),
            @Result(property = "orderNo", column = "order_no"),
            @Result(property = "mdId", column = "md_id"),
            @Result(property = "totalPrice", column = "total_price"),
            @Result(property = "orderStatus", column = "order_status"),
            @Result(property = "dateAdd", column = "create_time"),
            @Result(property = "subMchId", column = "subMchId"),
            @Result(property = "userCode", column = "user_code"),
            @Result(property = "phone", column = "phone")
    })
    public OrderShowVo selectOrderShowByOrderId(@Param("orderId")  Long orderId);

    /**
     * 用户确认支付，订单已完成
     * 更新：支付状态为已支付+订单状态为已完成，
     * @param userId 用户id
     * @param orderId 订单id
     * @return
     */
    @Update("update canteen_cp_order set pay_flag=1,order_status=5 where id=#{orderId} and user_id=#{userId}")
    public int confirmPayById(@Param("userId") Long userId,@Param("orderId")  Long orderId);

    /**
     * 用户确认支付，订单已完成
     * 更新：支付状态为已支付+订单状态为已完成
     * @param userId 用户id
     * @param orderNo 订单编号
     * @return
     */
    @Update("update canteen_cp_order set pay_flag=1,order_status=5 where order_no=#{orderNo} and user_id=#{userId}")
    public int confirmPayByNo(@Param("userId") Long userId,@Param("orderNo") String orderNo);


    /**
     * 用户确认支付，订单已完成
     * 更新：支付状态为已支付+订单状态为已完成
     * @param userCode 员工工号
     * @param orderNo 订单编号
     * @return
     */
    @Update("update canteen_cp_order set pay_flag=1,order_status=5,user_code=#{userCode} where order_no=#{orderNo} and " +
            "user_id in (SELECT id FROM canteen_user where user_code=#{userCode} and delete_time is null)")
    public int confirmPayByUserCodeAndNo(@Param("userCode") String userCode, @Param("orderNo") String orderNo);

    /**
     * 用户发起售后-退货申请
     * order_status=6 售后中（退货申请待审核）
     * @param orderId 订单id
     * @param userId  发起人用户id
     * @return
     */
    @Update("update canteen_cp_order set order_status=6 where id= #{orderId} and user_id=#{userId}")
    public int canncelOrderById(@Param("userId") Long userId, @Param("orderId") Long orderId);

    /**
     * 更新订单状态
     * order_status=7：交易关闭（退货审核不通过）11：售后中（退货已入库）
     * order_status=12：交易关闭（完成退款）
     * @param orderId 订单id
     * @param orderStatus  订单状态
     * @return
     */
    @Update("update canteen_cp_order set order_status=#{orderStatus} " +
            "where id= #{orderId}")
    public int updateOrderStatusById(@Param("orderStatus") Integer orderStatus, @Param("orderId") Long orderId);

    /**
     * 微信支付：用户确认支付，订单已完成
     * 更新：支付状态为已支付+订单状态为已完成
     * @param openid  用户openid
     * @param orderNo 订单编号
     * @param serialNo 微信支付成功返回值
     * @return
     */
    @Update("update canteen_cp_order set pay_flag=1,order_status=5,serial_no=#{serialNo} where order_no=#{orderNo} and " +
            "user_id = (SELECT id FROM canteen_user where openid=#{openid} and delete_time is null)")
    public int confirmPayByOpenId(@Param("openid") String openid,
                                  @Param("orderNo") String orderNo, @Param("serialNo") String serialNo);



    /**
     * 根据订单编号查询主订单详情
     * @param orderNo 订单编号
     * @return
     */
    @Select("SELECT id, order_no, order_type, md_id, user_id, user_code,deliver_link_man, phone, address, address_id, " +
            "total_price, pay_type, pay_flag, order_status, serial_no, meal_code, order_notes, " +
            "require_deliver_id, plan_deliver_time, real_deliver_time, create_time, sended, remark " +
            "from canteen_cp_order as o "+
            "where o.order_no = #{orderNo} ")
    @Results({
            @Result(property = "id", column = "id"),
            @Result(property = "orderNo", column = "order_no"),
            @Result(property = "mdId", column = "md_id"),
            @Result(property = "userId", column = "user_id"),
            @Result(property = "userCode", column = "user_code"),
            @Result(property = "deliverLinkMan", column = "deliver_link_man"),
            @Result(property = "phone", column = "phone"),
            @Result(property = "address", column = "address"),
            @Result(property = "addressId", column = "address_id"),
            @Result(property = "totalPrice", column = "total_price"),
            @Result(property = "serialNo", column = "serial_no"),
            @Result(property = "orderNotes", column = "order_notes"),
            @Result(property = "createTime", column = "create_time"),
            @Result(property = "remark", column = "remark"),
    })
    public CanteenCpOrder selectCpOrderByNo(@Param("orderNo") String orderNo);


}
